<?php
require_once("includes/include.php");
?>
<?php
class indexpage extends page {
	public $yanzhengma = "";
	public $errorMes = "";
	public $userNumber = "";
	public $sql;
	public $jumpUrl = "";//来自系统内部跳转url
	public function __construct(){
		parent::__construct();
		if ($_COOKIE){		
			if ($_COOKIE['userNumber']!=""&&$_COOKIE['userPwd']!=""){
				$post = $_COOKIE;	
				$this->login($post);
			}
		}
		$this->getYanZhengMa();//获取4位数字的随即数
		$errorType = $_GET["error"];//获取错误信息
		if ($errorType!=""){	
			$this->getErrorMes($errorType);
		}
		$this->userNumber= $_GET["userNumber"];//获取登陆失败的用户账号
		$this->jumpUrl = $_GET["jumpUrl"];//获取来自系统内部发生错误的url，成功登陆后自动跳转至此Url
		$post = $_POST["userNumber"];
		
		if ($post){
			$post = $_POST;
			$this->login($post);
		}
	}
	public function getYanZhengMa(){
		$this->yanzhengma = rand(1000,9999);//获取4位数字的随即数
	}
	public function getErrorMes($errorType){
		$errorMes = "";
		switch ($errorType){
			case 0:$errorMes = "用户账号不存在！";break;
			case 1:$errorMes = "用户密码错误！";break;
			case 2:$errorMes = "验证码错误！";break;
			case 3:$errorMes = "您还没有登陆，请先登陆！";break;
		}
		$this->errorMes = $errorMes;
	}
	public function login($post){
		$userNumber = $post["userNumber"];
		$userPwd = $post["userPwd"];
		$jumpUrl = $post["jumpUrl"];
		$remember = $post["remember"];
		$userNumber = $this->formDataFilter($userNumber);
		$userPwd = $this->formDataFilter($userPwd);
		$this->sql = "select count(id) from user where user_number = '$userNumber'";
		$dao = new indexdao();
		$checkUserNumber = $dao->checkUserNumber($userNumber);
		if ($checkUserNumber!=""){
			$checkUserPwd = $dao->checkUserPwd($userNumber,$userPwd);
			if ($checkUserPwd[0][0]!=""){
				//设置SESSION
				session_start();
				$_SESSION["userId"] = $checkUserPwd[0][0];
				$userId=$checkUserPwd[0][0];
				$_SESSION["userNumber"] = $checkUserPwd[0][1];
				$_SESSION["userName"] = $checkUserPwd[0][2];
				$_SESSION["userKind"] = $checkUserPwd[0][5];
				$dao->recordIp('user','last_login_ip',$userId);
				date_default_timezone_set("Asia/shanghai");
				$todayTime = date("Y-m-d H:i:s");
				$dao->updateLoginTime($userId,$todayTime);
				if ($jumpUrl != "") header("Location:$jumpUrl");
				else header("Location:bloglist.php?userId=$userId");
			}
			else {
				setcookie("userPwd","");
				header("Location:index.php?userNumber=$userNumber&&error=1");
			}
		}
		else {
			setcookie("userNumber","");
			setcookie("userPwd","");
			header("Location:index.php?userNumber=$userNumber&&error=0");
		}
	}
}
$page = new indexpage();
?>